/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

/**
 * <p>Project: jse2303 - CaptchaDemo
 * <p>Powered by webrx On 2023-07-25 08:50:33
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class CaptchaDemo {
    public static void main(String[] args) throws Exception {
        //建立图像
        int w = 160;
        int h = 50;
        int len = 4;
        BufferedImage i = new BufferedImage(w, h, 2);

        //获取画笔
        Graphics2D g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        //设置画笔颜色
        g.setColor(new Color(241, 241, 241));
        //绘制填充矩形
        g.fillRect(0, 0, w, h);

        String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sbu = new StringBuilder();
        Font font = new Font("宋体", Font.BOLD, 10);
        Random rand = new Random();
        //验证码文字
        //g.setColor(getRandColor(true));
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(180, 230)));
        for (int j = 0; j < len; j++) {
            font = font.deriveFont(Font.BOLD, rand.nextInt(20, 40));
            g.setFont(font);
            int x = w / 4 * j + 15;
            int y = rand.nextInt(25, 50);

            //设置每个字符的随机旋转
            double radianPercent = Math.PI * (rand.nextInt(35) / 180D);
            if (rand.nextBoolean()) radianPercent = -radianPercent;
            g.rotate(radianPercent, x, y);
            String c = String.valueOf(letter.charAt(rand.nextInt(letter.length())));
            sbu.append(c);
            g.drawString(c, x, y);
            g.rotate(-radianPercent, x, y);
        }

        //干扰文字
        int dot = rand.nextInt(5, 30);
        for (int j = 0; j < dot; j++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(80, 100)));
            font = font.deriveFont(Font.BOLD, rand.nextInt(10, 20));
            g.setFont(font);
            int x = rand.nextInt(w);
            int y = rand.nextInt(h);

            //设置每个字符的随机旋转
            double radianPercent = Math.PI * (rand.nextInt(35) / 180D);
            if (rand.nextBoolean()) radianPercent = -radianPercent;
            g.rotate(radianPercent, x, y);
            String c = String.valueOf(letter.charAt(rand.nextInt(letter.length())));
            g.drawString(c, x, y);
            g.rotate(-radianPercent, x, y);
        }

        System.out.println(sbu);


        //干扰线
        for (int i1 = 0; i1 < 15; i1++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(50, 90)));
            if (Math.random() > .8) {
                g.setColor(getRandColor(true));
            }
            g.setStroke(new BasicStroke(rand.nextInt(1, 6)));
            int x1 = rand.nextInt(w);
            int y1 = rand.nextInt(h);
            int x2 = rand.nextInt(w);
            int y2 = rand.nextInt(h);
            g.drawLine(x1, y1, x2, y2);
        }


        //释放画笔对象占用的内存资源
        g.dispose();

        //保存图像
        ImageIO.write(i, "png", new File("ck.png"));
    }


    public static Color getRandColor(boolean c) {
        Random rand = new Random();
        Color color = c ? new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)) : new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        return color;
    }

    public static Color getRandColor() {
        return getRandColor(false);
    }
}
